/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: log.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月23日 星期二 18时19分19秒
***************************************************/

#include "log.h"

void logMassage(LogLevel level,const char *format,...){
//初始化
	FILE *logfile = NULL;
	logfile = fopen(LOG_FILE,"a");
	if(logfile == NULL){
		fprintf(stderr,"Unable to open log file %s\n",LOG_FILE);
		exit(1);
	}

//写入日志
	
	//获取当前时间
	time_t now;
	time(&now);
	struct tm *local = localtime(&now);

	const char *levelString[] = {"ERROR","WARNING","INFO","DEBUG"};

	//将时间错误等级信息写入文件
	fprintf(logfile,"[%02d-%02d-%04d %02d:%02d:%02d] [%s]",local->tm_mday,local->tm_mon+1,\
			local->tm_year+1900,local->tm_hour,local->tm_min,local->tm_sec,levelString[level]);

	//处理可变参数写入文件
	va_list arg;
	va_start(arg,format);
	vfprintf(logfile,format,arg);
	va_end(arg);

	//换行并刷新缓存区
	fprintf(logfile,"\n");
	fflush(logfile);

//关闭日志文件
	if(logfile != NULL){
		fclose(logfile);
		logfile = NULL;
	}
}
